﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using TanWeb.Helpers;
using TanWeb.Models.Entities;
using TanWeb.Models.DataAccess.Infrastructure;

namespace TanWeb.Models.DataAccess
{
    public class MasterDataAccess : BaseDataAccess
    {
        //===============================================================
        //  コンストラクタ等宣言
        //===============================================================
        #region /***    Constructor Destructor Declared    ***/

        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="dbContext">データベースコンテキスト</param>
        public MasterDataAccess(DbContext dbContext) : base(dbContext) { }

        #endregion

        public int GetMasterId(string user, string encryptedPassword)
        {
            // ログを記述する
            logger.Info("\t\tStart MasterDataAccess - GetMaster");
            int result = 0;
            string sql = string.Empty;

            try
            {
                sql = string.Format(
                            @"SELECT id
                            FROM Master
                            WHERE [Username] = '{0}'
                            AND [Password] = '{1}'", user, encryptedPassword);
                result = this.DbContext.Database.SqlQuery<int>(sql).First();
            }
            catch (Exception ex)
            {
                logger.Error("\t\tError: MasterDataAccess - GetMaster: ", ex);
                throw ex;
            }

            logger.Info("\t\tEnd MasterDataAccess - GetMaster");

            return result;
        }
    }
}